package com.geek.gkfgw.web.model;

import com.geek.gkfgw.facade.abs.Response;
import com.geek.gkfgw.web.local.ResponseThreadLocal;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.time.FastDateFormat;

import java.util.Date;

/**
 * <h1>响应统一格式对象。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2022-02-19
 *
 */
@Setter
@Getter
@ToString(callSuper = true)
public class BaseResponse<T> extends Response<T> {

    private static final FastDateFormat FMT_INSTANCE = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSSSS");

    public BaseResponse() {
        ResponseThreadLocal.set(this);
    }

    public BaseResponse(String code, String memo) {
        this.code = code;
        this.memo = memo;
        this.time = FMT_INSTANCE.format(new Date());
        ResponseThreadLocal.set(this);
    }

    public BaseResponse(T data) {
        this.data = data;
        this.time = FMT_INSTANCE.format(new Date());
        ResponseThreadLocal.set(this);
    }

    public BaseResponse(String code, String memo, T data) {
        this.code = code;
        this.memo = memo;
        this.data = data;
        this.time = FMT_INSTANCE.format(new Date());
        ResponseThreadLocal.set(this);
    }

    public static Response ok() {
        return new BaseResponse(null);
    }

    public static Response ok(Object resp) {
        return new BaseResponse(resp);
    }

    public static Response exception() {
        return new BaseResponse("9999", "系统异常", null);
    }
}